package com.wanqing.community.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanqing.community.util.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wanqing.community.bean.MenuBean;
import com.wanqing.community.bean.RoleBean;
import com.wanqing.community.controller.util.BaseController;
import com.wanqing.community.manager.MenuManager;
import com.wanqing.community.manager.RoleManager;

/**
 * 
 * All right reserved.
 * @version 
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController{

	private static Logger log = Logger.getLogger(RoleController.class);
	
	@Resource
	private RoleManager roleManager;
	
	@Resource
	private MenuManager menuManager;
	
	@RequestMapping(params="method=index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView();
		String type = StringTools.objectToString(request.getParameter("type"));
		if("list".equals(type)){
			mav.setViewName("sys/role/listRole");
		}else if("author".equals(type)){
			String roleId = StringTools.objectToString(request.getParameter("roleId"));
			String roleName = StringTools.objectToString(request.getParameter("roleName"));
			List<MenuBean> list = this.menuManager.listAuthorMenu();
			mav.addObject("menus", list);
			mav.addObject("roleId", roleId);
			mav.addObject("roleName", roleName);
			mav.setViewName("sys/role/listAuthors");
		}
		return mav;
	}
	
	/**
	 * <p>Description:查询角色列表<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=queryRole")
	public String queryRole(HttpServletRequest request,HttpServletResponse response){
		try{
			String roleName = StringTools.objectToString(request.getParameter("roleName"));
			int pageNo = NumberDealTools.stringTranceInt(request.getParameter("page"), 1);
			System.out.print(request.getParameter("rows")+"@@@@@@@@@@@");
			int pageSize = NumberDealTools.stringTranceInt(request.getParameter("rows"), KeyConstant.PAGESIZE);
			ConditionParse condParse = new ConditionParse();	
			
			//查询条件
			StringBuffer cStr = new StringBuffer();
			if(!roleName.trim().equals("")){
				cStr.append(" AND ROLENAME LIKE '%"+roleName+"%'");
			}
			if(cStr.length() != 0){
				condParse.addConditionStr(cStr.toString().substring(4));
			}
			
			condParse.addWhereStr();
			int total = this.roleManager.getRoleCount(condParse);
			List<RoleBean> list = this.roleManager.queryRole(condParse, pageNo, pageSize);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("total", total);
			map.put("rows", list);	
			String json = StringTools.buildJson(map);			
			writeJson(response, json);
		}catch (Exception e) {
			log.error("查询角色列表异常："+e.toString());
		}
		return null;
	}
	
	/**
	 * <p>Description:增加角色<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=addRole")
	public String addRole(HttpServletRequest request,HttpServletResponse response){
		try{
			String roleName = StringTools.objectToString(request.getParameter("roleName"));
			String remarks = StringTools.objectToString(request.getParameter("remarks"));
			RoleBean role = new RoleBean();
			role.setRemarks(remarks);
			role.setRoleName(roleName);
			role.setRoleId(UUID.uuId());
			boolean b = this.roleManager.addRole(role);
			if(b){
				writeText(response, KeyConstant.SUCCESS);
			}else{
				writeText(response, KeyConstant.FAIL);
			}
		}catch (Exception e) {
			log.error("增加角色异常："+e.toString());
			writeText(response, e.toString());
		}
		return null;
	}
	
	/**
	 * <p>Description:修改角色<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=updateRole")
	public String updateRole(HttpServletRequest request,HttpServletResponse response){
		try{
			String roleName = StringTools.objectToString(request.getParameter("roleName"));
			String remarks = StringTools.objectToString(request.getParameter("remarks"));
			String roleId = StringTools.objectToString(request.getParameter("roleId"));
			RoleBean role = new RoleBean();
			role.setRemarks(remarks);
			role.setRoleName(roleName);
			role.setRoleId(roleId);
			boolean b = this.roleManager.updateRole(role);
			if(b){
				writeText(response, KeyConstant.SUCCESS);
			}else{
				writeText(response, KeyConstant.FAIL);
			}
		}catch (Exception e) {
			log.error("修改角色异常："+e.toString());
			writeText(response, e.toString());
		}
		return null;
	}
	
	/**
	 * <p>Description:删除角色<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=deleteRole")
	public String deleteRole(HttpServletRequest request,HttpServletResponse response){
		try{			
			String[] roleIds = request.getParameterValues("id[]");	
			if(null==roleIds || roleIds.length==0){
				writeText(response, KeyConstant.EMPTY);
			}else{
				List<String> list = Arrays.asList(roleIds);
				boolean b = this.roleManager.deleteRole(list);	
				if(b){
					writeText(response, KeyConstant.SUCCESS);
				}else{
					writeText(response, KeyConstant.FAIL);
				}
			}
		}catch (Exception e) {
			log.error("删除角色异常："+e.toString());
			writeText(response, e.toString());
		}
		return null;
	}
	
	/**
	 * <p>Description:角色授权<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=auhorRole")
	public String auhorRole(HttpServletRequest request,HttpServletResponse response){
		try{
			String roleId = StringTools.objectToString(request.getParameter("roleId"));		
			String[] listOperId = request.getParameterValues("listOperId");			
			if(null!=listOperId && listOperId.length>0){
				List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
				Map<String, Object> map = null;
				for (String operaId : listOperId) {
					map = new HashMap<String, Object>();
					String systemId = StringTools.objectToString(request.getParameter("systemId_"+operaId));
					String menuId = StringTools.objectToString(request.getParameter("menuId_"+operaId));
					map.put("systemId", systemId);
					map.put("menuId", menuId);
					map.put("operaId", operaId);
					map.put("roleId", roleId);
					map.put("aclVal", 1);
					list.add(map);
				}
				boolean b = this.roleManager.addRoleAuthor(list);
				if(b){
					writeText(response, KeyConstant.SUCCESS);
				}else{
					writeText(response, KeyConstant.FAIL);
				}
			}
		}catch (Exception e) {
			log.error("角色授权异常："+e.toString());
			writeText(response, e.toString());
		}
		return null;
	}
	
	/**
	 * <p>Description:给权限组授系统菜单权限时查询操作按钮<p>
	 * <p>modifiContent:<p>
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=queryFuncOperAuthor")
	public String queryFuncOperAuthor(HttpServletRequest request,HttpServletResponse response){
		String roleId = StringTools.objectToString(request.getParameter("roleId"));				
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			List<RoleBean> list = this.roleManager.queryOperaAuthorRole(roleId);
			if(null==list || list.isEmpty()){
				map.put("errors", 1);				
			}else{
				map.put("errors", 0);
				map.put("rows", list);
			}
		}catch (Exception e) {			
			log.error("给权限组授系统菜单权限时查询操作按钮异常："+e.toString());
			map.put("errors", e.toString());	
		}
		String content = StringTools.buildJson(map);		
		writeJson(response, content);
		return null;
	}
}
